package com.longer.demo.biz.controller;

import com.longer.demo.biz.service.OrderService;
import com.longer.demo.biz.service.model.OrderModel;
import com.longer.demo.biz.service.model.UserModel;
import com.longer.demo.framework.controller.BaseController;
import com.longer.demo.framework.error.BusinessError;
import com.longer.demo.framework.error.BusinessException;
import com.longer.demo.framework.reponse.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: mybatis
 * @description:
 * @author: SunBiaoLong
 * @create: 2019-01-15 08:10
 **/
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController{

	@Autowired
	private HttpServletRequest httpServletRequest;

	@Autowired
	private OrderService orderService;

	@PostMapping(value = "/create",consumes = {CONTENT_TYPE_FORMED})
	public CommonResponse createOrder(@RequestParam(name = "itemId") Integer itemId,
	                                  @RequestParam(name = "account") Integer account) throws BusinessException {
		Boolean isLogin = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
		if (isLogin==null||!isLogin) {
			throw new BusinessException(BusinessError.USER_NOT_LOGIN, "用户还没登陆,不能下单");
		}

		UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");

		OrderModel orderModel = orderService.createOrder(itemId, userModel.getId(), account);
		return CommonResponse.create(orderModel);
	}
}
